//
//  GXProviderDetailTableViewCell.m
//  GxLanOuProject
//
//  Created by 甘霞 on 15/10/24.
//  Copyright © 2015年 甘霞. All rights reserved.
//

#import "GXProviderDetailTableViewCell.h"
//#import "<#header#>"
#import "GXProviderDetailModel.h"
#import "NSString+GXSizeWithText.h"
#import "UIImageView+WebCache.h"
#import "GXSuggestModel.h"
#import "UIImage+GIF.h"

//边距
#define kProviderSizeBorder 10

//图片宽
#define kDetailImageViewWidth 140

//标题与图片之间的间隔
#define kDetailTitleLabelLeftBoder 15
//标题与专题的距离
#define kDetailTItleLabelTopBorder 10
//名字与标题的距离
#define kDetailNameLabelTopBorder 10

//专题高度
#define kDetailSpecialLabelHeight 20

@interface GXProviderDetailTableViewCell ()


@end

@implementation GXProviderDetailTableViewCell

#pragma mark - 初始化控件
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubviews];
    }
    return self;
}

#pragma mark - 创建子控件
- (void)createSubviews {
    
    BOOL isNight = [[NSUserDefaults standardUserDefaults] boolForKey:@"isNight"];
    
    self.backgroundColor = isNight ? [UIColor blackColor] : [UIColor whiteColor];
    
    //图片
    self.detailImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:self.detailImageView];
    self.detailImageView.contentMode = UIViewContentModeScaleAspectFill;
    self.detailImageView.layer.masksToBounds = YES;
    //专题
    self.specialLabel = [[UILabel alloc] init];
    [self.contentView addSubview:self.specialLabel];
    self.specialLabel.textColor = [UIColor darkGrayColor];
    [self.specialLabel setFont:[UIFont systemFontOfSize:kSuggestTitleFontSize]];
    
    //标题
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.textColor = [UIColor blackColor];
    [self.titleLabel setFont:[UIFont systemFontOfSize:kSuggestTitleFontSize]];
    [self.titleLabel setNumberOfLines:3];
    [self.contentView addSubview:self.titleLabel];
    
    //名字
    self.nameLabel = [[UILabel alloc] init];
    [self.nameLabel setTextColor:[UIColor colorWithRed:35/255.0 green:172/255.0 blue:183/255.0 alpha:1]];
    [self.nameLabel  setFont:[UIFont systemFontOfSize:kSuggestAuthorFontSize]];
    [self.contentView addSubview:self.nameLabel];
}

// 布局
- (void)layoutSubviews {
    [super layoutSubviews];
    
    //图片
    [self.detailImageView setFrame:CGRectMake(kProviderSizeBorder, kProviderSizeBorder, kDetailImageViewWidth, self.contentView.frame.size.height - 2 * kProviderSizeBorder)];
    
    //专题
    CGFloat specialLabelX = CGRectGetMaxX(self.detailImageView.frame) + kDetailTitleLabelLeftBoder;
    CGFloat specialLabelW = self.contentView.frame.size.width - 2 * kProviderSizeBorder - kDetailTitleLabelLeftBoder - kDetailImageViewWidth;
    [self.specialLabel setFrame:CGRectMake(specialLabelX, self.detailImageView.frame.origin.y, specialLabelW, kDetailSpecialLabelHeight)];
    
    //标题
    CGFloat titleLabelY = CGRectGetMaxY(self.specialLabel.frame) + kDetailTItleLabelTopBorder;
    CGRect titleLabelSize = [self.titleLabel.text SizeWithTextFontSize:kSuggestTitleFontSize textWidth:specialLabelW];
    //如果标题长度超过3行 文本会向下沉 界面不整齐 则如果超过3行 设置固定高度 让标题文本居上
    CGFloat titleLabelHeight = titleLabelSize.size.height > 3 * 25 ? 3 * 25 : titleLabelSize.size.height;
    [self.titleLabel setFrame:CGRectMake(specialLabelX, titleLabelY, specialLabelW, titleLabelHeight)];
    
    //名字
    CGFloat detailImageViewY = CGRectGetMaxY(self.detailImageView.frame);
    CGFloat nameLabelY = CGRectGetMaxY(self.titleLabel.frame) + kDetailNameLabelTopBorder;
    //如果名字的Y超过图片的最大Y值 则名字Y = 图片Y
    CGFloat nameLabel =  nameLabelY > detailImageViewY - kDetailSpecialLabelHeight ? detailImageViewY - kDetailSpecialLabelHeight : nameLabelY;
    [self.nameLabel setFrame:CGRectMake(specialLabelX, nameLabel, specialLabelW, kDetailSpecialLabelHeight)];
}

#pragma mark - 控件赋值
- (void)setDetailModel:(GXProviderDetailModel *)detailModel {
    if (_detailModel != detailModel) {
        _detailModel = detailModel;
    }
    //图片
    [self.detailImageView sd_setImageWithURL:_detailModel.image[@"raw"] placeholderImage:[UIImage sd_animatedGIFNamed:@"loading"]];
    //专题
    self.specialLabel.text = @"专题";
    //标题
    [self.titleLabel setText:_detailModel.title];
    //名字
    [self.nameLabel setText:_detailModel.author];
}

//如果作为推荐中的文章 cel 则使用这个模型
- (void)setSuggestModel:(GXSuggestModel *)suggestModel {
    if (_suggestModel != suggestModel) {
        _suggestModel = suggestModel;
    }
    //大图
    [self.detailImageView sd_setImageWithURL:_suggestModel.thumb[@"raw"] placeholderImage:[UIImage sd_animatedGIFNamed:@"loading"]];
    //专题
    self.specialLabel.text = _suggestModel.type_name;
    //标题
    self.titleLabel.text = _suggestModel.title;
    //姓名
    self.nameLabel.text = _suggestModel.cp[@"screen_name"];
}

- (void)setCoreSugModel:(SuggestModel *)coreSugModel
{
    if (_coreSugModel != coreSugModel) {
        _coreSugModel = coreSugModel;
    }
    [self.detailImageView sd_setImageWithURL:[NSURL URLWithString:coreSugModel.share_image]];
    self.specialLabel.text = [[NSString alloc] initWithData:coreSugModel.cp encoding:NSUTF8StringEncoding];
    self.titleLabel.text = coreSugModel.title;
    self.nameLabel.text = coreSugModel.intro;
}

@end
